package cn.bdqn.service.impl;

import cn.bdqn.domain.Menber;
import cn.bdqn.domain.Recipe;
import cn.bdqn.mapper.MenberMapper;
import cn.bdqn.mapper.RecipeMapper;
import cn.bdqn.service.RecipeService;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 处方业务实现
 */
@Service
public class RecipeServiceImpl implements RecipeService {

    @Autowired
    private RecipeMapper recipeMapper;

    @Autowired
    private MenberMapper menberMapper;

    @Autowired
    private AmqpTemplate amqpTemplate;

    /**
     * 查询所有的处方
     * @return
     */
    @Override
    public List<Recipe> queryAll() {
        return recipeMapper.selectAll();
    }

    /**
     * 修改处方的抓药状态
     * @param id 处方对象
     */
    @Override
    @Transactional
    public int editRecipeById(Integer id) {

        Recipe recipe = recipeMapper.selectByPrimaryKey(id);
        Menber menber = menberMapper.selectAllByMenberIdcard(recipe.getRegisId());
        if (recipe.getDrugTotalprice()>menber.getMenberPrestore()){
            return -1;
        }else {
            amqpTemplate.convertAndSend("", "PM-Queue", recipe);
            return recipeMapper.updateIfallocationById(id);
        }
    }
}
